package com.example.java.newFeature;

public class SwitchDemo {
    public static void main(String[] args) {
        int day = 3;
        String dayName = switch (day) {
            case 1 -> "Monday";
            case 2 -> "Tuesday";
            case 3 -> "Wednesday";
            case 4 -> "Thursday";
            case 5 -> "Friday";
            case 6 -> "Saturday";
            case 7 -> "Sunday";
            default -> throw new IllegalArgumentException("Invalid day: " + day);
        };
        System.out.println(dayName); // 输出: Wednesday



        char grade = 'B';
        String evaluation = switch (grade) {
            case 'A', 'B' -> "Excellent";
            case 'C' -> "Good";
            case 'D' -> "Fair";
            case 'F' -> "Poor";
            default -> "Invalid grade";
        };
        System.out.println(evaluation); // 输出: Good



        String fruit = "Apple";
        String color = switch (fruit) {
            case "Apple" -> {
                System.out.println("It's an Apple");
                yield "Red"; // yield 语句用于返回值
            }
            case "Banana" -> {
                System.out.println("It's a Banana");
                yield "Yellow"; // yield 语句
            }
            default -> {
                System.out.println("Unknown fruit");
                yield "Unknown";
            }
        };

        System.out.println(color); // 输出: Red
    }
}
